package cn.qdhlm.collectservice.core.component.analyse;

import cn.qdhlm.collectservice.common.Constant;
import cn.qdhlm.collectservice.common.util.CodeUtil;
import cn.qdhlm.collectservice.common.util.ComUtil;

import cn.qdhlm.collectservice.service.UtilityService;
import io.netty.buffer.ByteBuf;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Name: AnyAfn05
 * @Description: 读参数（AFN=05H）
 * @Author: jiangcs
 */
@Log4j2
@Component
public class AnyAfn05 {
    @Autowired
    private UtilityService utilityService;

    public boolean execute(int da, int dt, int sn, String collectorNo, ByteBuf byteBuf) throws Exception {
        boolean executeFlag = false;

        // F13：读VAC200OC阀门配置
        if (da == Constant.DA_0 && dt == Constant.DT_4097) {
            Map<String, Object> resultMap = new HashMap<>(12);

            // 终端编号
            StringBuilder sbTerminalNo = new StringBuilder();
            for (int i = 0; i < Constant.BYTE_LENGTH_TERMINAL_NO; i++) {
                String asciiStr = CodeUtil.byteToHexString(byteBuf.readByte());
                if (Constant.SYMBOL_00.equals(asciiStr)) {
                    // 跳过剩余字节
                    byteBuf.skipBytes(Constant.BYTE_LENGTH_TERMINAL_NO - i - 1);
                    break;
                }
                sbTerminalNo.append(CodeUtil.hexStringToAsciiString(asciiStr));
            }
            String terminalNo = sbTerminalNo.toString();

            // 温度上限
            resultMap.put("temperatureUpper", byteBuf.readShortLE() * 0.01);
            // 温度下限
            resultMap.put("temperatureLower", byteBuf.readShortLE() * 0.01);
            // 采暖季开始
            String startDay = CodeUtil.byteToHexString(byteBuf.readByte());
            String startMonth = CodeUtil.byteToHexString(byteBuf.readByte());
            resultMap.put("heatSeasonStart", startMonth + "-" + startDay);
            // 采暖季结束
            String endDay = CodeUtil.byteToHexString(byteBuf.readByte());
            String endMonth = CodeUtil.byteToHexString(byteBuf.readByte());
            resultMap.put("heatSeasonEnd", endMonth + "-" + endDay);
            // 清洗周期：分钟
            resultMap.put("cleanCycle", byteBuf.readShortLE());
            // 开度上限
            resultMap.put("openDegreeUpper", byteBuf.readByte());
            // 开度下限
            resultMap.put("openDegreeLower", byteBuf.readByte());
            // 调节模式：0-面板控制；1-远程控制开度；2-回水温度控制开度；3-进回水温差控制开度
            int adjustMode = byteBuf.readByte();
            resultMap.put("adjustMode", adjustMode);
            // 目标调节值 50%开度，调节模式不同含义不同，调节模式为00：此处无用；01：表目标开度；02：表目标温度；03：表目标温差
            Object adjustValue = byteBuf.readShortLE();
            if (adjustMode == Constant.VALVE_ADJUST_MODE_2 || adjustMode == Constant.VALVE_ADJUST_MODE_3) {
                adjustValue = ComUtil.safeDouble(adjustValue) * 0.01;
            }
            resultMap.put("adjustValue", adjustValue);
            // 是否启用自动调平：0-启用；1-不启用
            resultMap.put("autoAdjust", byteBuf.readByte());
            // 调节周期：分钟
            resultMap.put("adjustCycle", byteBuf.readShortLE());

            // 保存终端参数表
//            utilityService.saveTerminalConfigParam(collectorNo.getId(), terminalNo, resultMap);

            // 更新命令结果状态
            resultMap.put("statusCode", Constant.SYMBOL_00);
//            utilityService.updateCommandResultStatus("terminal", collectorNo.getCollectorNo(), sn, resultMap, 2);

            executeFlag = true;
        }
        // F17：读版本号
        else if (da == Constant.DA_0 && dt == Constant.DT_258) {
            int versionLength = byteBuf.readByte();
            StringBuilder sbVersion = new StringBuilder();
            String hexStr;
            for (int i = 0; i < versionLength; i++) {
                hexStr = CodeUtil.byteToHexString(byteBuf.readByte());
                sbVersion.append(CodeUtil.hexStringToAsciiString(hexStr));
            }

            // 确认命令结果状态
            Map<String, Object> resultMap = new HashMap<>(2);
            resultMap.put("statusCode", Constant.SYMBOL_00);
            resultMap.put("version", sbVersion);
//            utilityService.updateCommandResultStatus("collector", collectorNo.getCollectorNo(), sn, resultMap, 2);

            executeFlag = true;
        } else {
            log.error("execute da[{}], dt[{}] match fail", da, dt);
        }

        return executeFlag;
    }
}
